// pages/appendaddress/appendaddress.js
var app = getApp();
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**是否显示保存成功提示框  */
    isSaveSucceed: false,
    isShowSelAddress: false,
    switch: 1,
    isClickSet: true,
    selTypeArr: [{ name: "省", strip1: false }, { name: "市", strip1: true }, { name: "区县", strip1: true }],
    province: [],
    provinceAddress: "请选择",
    endAddress: "",
    //   isSelArea = false
    inputName: "",
    inputPhone: "",
    inputAddress: '',
    /**标题 */
    title: "",
    allAddress: "",
    /**修改的收货地址 */
    editInfo: {},
    provinceStr: "",
    areaStr: "",
    inputData: { "name": "", "phone": 0, "addres": "", "areas": "" },
    isMunicipality: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /**获取当前位置（ 省份 城市 地区） */
    // var a = remote_ip_info;
    let title = GlobalData.addressTitle > 0 ? "编辑收货地址" : "添加收货地址";
    wx.setNavigationBarTitle({
      title: title,
    })
    if (GlobalData.editInfo && GlobalData.addressTitle) {
      // this.editInfo = GlobalData.clone(GlobalData.editInfo);
      this.data.inputName = GlobalData.editInfo.name;
      this.data.provinceAddress = GlobalData.editInfo.areas;
      this.data.inputPhone = GlobalData.editInfo.phone;
      this.data.inputAddress = GlobalData.editInfo.address;
      this.setData({
        inputName: this.data.inputName,
        provinceAddress: this.data.provinceAddress,
        inputPhone: this.data.inputPhone,
        inputAddress: this.data.inputAddress,
      })
    }

    for (var code in app.getCitysData()) {
      if (!(parseInt(code) % 1e4)) {
        let obj = [];
        obj.push(code);
        obj.push(app.getCitysData()[code]);
        GlobalData.CityData.provinceArr.push(obj);
      }
    }
    this.data.province = GlobalData.clone(GlobalData.CityData.provinceArr);
    this.setData({
      province: this.data.province,
      inputName: this.data.inputName,
      provinceAddress: this.data.provinceAddress,
      inputPhone: this.data.inputPhone,
      inputAddress: this.data.inputAddress,

    })
    // this.onSelArea();
  },

  /**获取地理位置信息 */
  onSelArea(index, areaType) {
    let obj = [];
    let a = areaType / 1e4
    if (a == 11 || a == 12 || a == 31 || a == 50 || a == 81 || a == 82) {
      GlobalData.CityData.citysArr = [];
      this.data.isMunicipality = true;
      let ty = areaType / 1e4;
      for (var code in app.getCitysData()) {
        let c = parseInt(code);
        let num = (c / 1e4) + "";

        if (ty == parseInt(num)) {
          if (c % 1e4) {
            obj.push(code);
            obj.push(app.getCitysData()[code]);
            GlobalData.CityData.citysArr.push(obj);
            obj = [];
          }

        }
      }
      this.data.province = GlobalData.clone(GlobalData.CityData.citysArr)
      this.setData({
        province: this.data.province,
      })

    } else {

      /**判断传进来的是省份还是市区 */
      if (areaType % 1e4 == 0) {
        GlobalData.CityData.citysArr = [];
        //传进来的是省份
        let ty = areaType / 1e4;
        for (var code in app.getCitysData()) {
          let c = parseInt(code);
          let num = (c / 1e4) + "";
          if (ty == parseInt(num)) {
            if (((c - areaType) % 1e2 == 0) && (c % 1e4 != 0)) {
              obj.push(code);
              obj.push(app.getCitysData()[code]);
              GlobalData.CityData.citysArr.push(obj);
              obj = [];
            }
          }

        }
        this.data.province = GlobalData.clone(GlobalData.CityData.citysArr);
        this.setData({
          province: this.data.province,
        })
      } else {
        /**
         * 传进来的是城市
         */
        GlobalData.CityData.areasArr = [];
        let num = areaType / 1e2 + ""
        for (var code in app.getCitysData()) {
          let c = parseInt(code);
          let ty = c / 1e2 + "";
          if (parseInt(num) == parseInt(ty)) {
            if (parseInt(num) < (c / 1e2)) {
              obj.push(code);
              obj.push(app.getCitysData()[code]);
              GlobalData.CityData.areasArr.push(obj);
              obj = [];
            }
          }
        }
        this.data.province = GlobalData.clone(GlobalData.CityData.areasArr);
        this.setData({
          province: this.data.province,
        })
      }
      this.data.isMunicipality = false;
    }
  },


  onShowSelAddress() {

    this.data.isShowSelAddress = !this.data.isShowSelAddress;
    this.data.province = GlobalData.clone(GlobalData.CityData.provinceArr);

    this.setData({
      isShowSelAddress: this.data.isShowSelAddress,
      province: this.data.province,
    })
  },
  onSetDefault() {
    this.data.isClickSet = !this.data.isClickSet;
    if (this.data.isClickSet) {
      this.data.switch = 1
    } else {
      this.data.switch = 0
    }
    this.setData({
      switch: this.data.switch,
    })
  },
  onCloseArea() {

    this.data.isShowSelAddress = false;
    this.data.province = GlobalData.clone(GlobalData.CityData.provinceArr);
    this.setData({
      isShowSelAddress: this.data.isShowSelAddress,
      province: this.data.province,
    })
  },
  /**选择省份城市地区 */
  onSelProvince(e) {
    let code = e.currentTarget.dataset.item;
    var strip1 = 0;
    for (let i = 0; i < this.data.selTypeArr.length; i++) {
      if (!this.data.selTypeArr[i].strip1) {
        strip1 = i;
      }
    }
    this.data.endAddress += code[1];
    if (this.data.isMunicipality && strip1 == 1) {
      this.data.selTypeArr[0].strip1 = false;
      this.data.selTypeArr[1].strip1 = true;
      this.data.selTypeArr[2].strip1 = true;
      this.data.selTypeArr[2].name = "区县";
      this.onCloseArea()
      this.data.provinceAddress = this.data.endAddress;
      this.data.inputData.areas = this.data.provinceAddress;
      this.data.endAddress = "";
      this.setData({
        selTypeArr: this.data.selTypeArr,
        isMunicipality: this.data.isMunicipality,
        provinceAddress: this.data.provinceAddress,
        inputData: this.data.inputData,
        endAddress: this.data.endAddress
      })
      return;
    }

    this.onSelArea(strip1, code[0]);

    // if (this.endAddress && strip1 == 0) {
    //   this.endAddress = "";
    //   this.provinceAddress = this.endAddress;
    //   this.inputData.areas = this.provinceAddress;
    //   // this.selTypeArr = [{ name: "省", strip1: false }, { name: "市", strip1: true }, { name: "区县", strip1: true }];
    // }

    if (strip1 == this.data.selTypeArr.length - 1) {
      this.data.selTypeArr[0].strip1 = false;
      this.onCloseArea()
      this.data.provinceAddress = this.data.endAddress;
      this.data.inputData.areas = this.data.provinceAddress;
      this.data.endAddress = "";
    } else
      this.data.selTypeArr[strip1 + 1].strip1 = false;
    this.data.selTypeArr[strip1].strip1 = true;
    this.data.selTypeArr[strip1].name = code[1];

    this.setData({
      selTypeArr: this.data.selTypeArr,
      isMunicipality: this.data.isMunicipality,
      provinceAddress: this.data.provinceAddress,
      inputData: this.data.inputData,
      endAddress: this.data.endAddress
    })
  },
  /**输入信息：1 输入名字  2 输入手机号  3输入街道*/
  inputBlur(e) {
    var txt = e.detail.value.trim();
    if (txt.length > 50) {
      return;
    }
    this.data.inputAddress = txt;
    this.data.inputData.address = this.data.inputAddress;
    this.data.areaStr = this.data.inputAddress;
    this.setData({
      inputAddress: this.data.inputAddress,
      inputData: this.data.inputData,
      areaStr: this.data.areaStr

    })
  },
  inputNameBlur(e) {
    var code = e.detail.value.trim();
    this.data.inputName = code;
    this.data.inputData.name = this.data.inputName
    this.setData({
      inputData: this.data.inputData,
    })
  },
  inputPhoneBlur(e) {
    var num = parseInt(e.detail.value);
    if (num == 0) {
      return "";
    }
    this.data.inputPhone = num
    if (!this.data.inputPhone || (this.data.inputPhone.toString().length != 11)) {
      app.onshowModal("请输入11位手机号码。");
      // return;
    }
    this.data.inputData.phone = this.data.inputPhone;
    this.setData({
      inputData: this.data.inputData,
    })
  },
  /**保存添加的收货地址 */
  onSaveAddress() {
    let obj = {};
    if (GlobalData.editInfo && GlobalData.addressTitle) {
      obj["id"] = GlobalData.editInfo.id;
      obj["name"] = this.data.inputData.name ? this.data.inputData.name : GlobalData.editInfo.name;
      obj["phone"] = this.data.inputData.phone ? this.data.inputData.phone : GlobalData.editInfo.phone;
      obj["address"] = this.data.inputData.address ? this.data.inputData.address : GlobalData.editInfo.address;
      obj["areas"] = this.data.inputData.areas ? this.data.inputData.areas : GlobalData.editInfo.areas;
    } else {
      obj["id"] = GlobalData.addressTitle;
      obj["name"] = this.data.inputData.name;
      obj["phone"] = this.data.inputData.phone;
      obj["address"] = this.data.inputData.address;
      obj["areas"] = this.data.inputData.areas
      // obj["area"] = this.areaStr ? this.areaStr : GlobalData.editInfo.areas;
    }
    obj["isdefault"] = this.data.switch;
    if (!obj["name"]) {
      app.onshowModal("请输入名字。");
      // this.app.show("提示", "请输入名字。", "确认");
      return;
    }
    if (!obj["phone"] || (obj["phone"].toString().length != 11)) {
      app.onshowModal("请输入11位手机号码。");
      // this.app.show("提示", "请输入11位手机号码。", "确认");
      return;
    }
    if (!obj["areas"]) {
      app.onshowModal("请选择省份城市。");
      // this.app.show("提示", "请选择省份城市。", "确认");
      return;
    }
    if (!obj["address"]) {
      app.onshowModal("请输入详细地址。");
      // this.app.show("提示", "请输入详细地址。", "确认");
      return;
    }


    this.onExchangeData(obj);
  },
  /**更新收货地址 */
  onExchangeData(data) {
    dataManager.rigisterObserver("updateshippingaddress", this.onComplete, this);
    dataManager.onEditAddressDm(data);
  },
  /**获取收货地址全部信息 */
  onComplete() {
    dataManager.rigisterObserver("getshippingaddress", this.onAddressComplete, this);
    dataManager.getAddressData()
  },

  onAddressComplete() {
    // this.app.show("提示", "保存成功", "确认");
    this.data.isSaveSucceed = true;
    GlobalData.editInfo = null;
    this.setData({
      isSaveSucceed: this.data.isSaveSucceed
    })
    wx.showModal({
      title: '提示',
      content: "保存成功",
      showCancel: false,
      success: function (res) {
        //返回上一级
        wx.navigateBack({
          delta: 1
        })
      }
    })


  },
  /**关闭提示框 */
  onClose() {
    this.data.isSaveSucceed = false;
    this.setData({
      isSaveSucceed: this.data.isSaveSucceed
    })
  },




  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})